﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;
using ASPNETVN.PORTAL.Components.UI;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class CooperativeList : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                for (int i = 1975; i <= DateTime.Today.Year; i++)
                {
                    ddlHistoryYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ddlHistoryYear.SelectedValue = (DateTime.Today.Year - 1).ToString();

                this.Display("AREA");
            }
        }

        private void Display(string groupField)
        {
            const string where = "1 = 1";
            //string where = string.Format("Year = {0}", ddlHistoryYear.SelectedValue);
            DataSet ds = Cooperative.Search(this.ModuleID, where);

            DataView dv = ds.Tables[0].DefaultView;

            switch (groupField)
            {
                case "AREA":
                    rptGroup.DataSource = Area.SelectCollectionAll(this.ModuleID);
                    break;
                case "CAREER":
                    rptGroup.DataSource = Career.SelectCollectionAll(this.ModuleID);
                    break;
                case "TYPE":
                    rptGroup.DataSource = Components.Type.SelectCollectionAll(this.ModuleID);
                    break;
            }
            rptGroup.DataBind();

            foreach (RepeaterItem item in rptGroup.Items)
            {
                GridView gv = (GridView)item.FindControl("gvCooperative");
                HiddenField hf = (HiddenField)item.FindControl("hfID");
                Label lblCountOfCooperative = (Label)item.FindControl("lblCountOfCooperative");
                Label lblSumOfLabourQuantity = (Label)item.FindControl("lblSumOfLabourQuantity");
                Label lblTotalOfCharterCapital = (Label)item.FindControl("lblTotalOfCharterCapital");
                Label lblTotalOfFixedCapital = (Label)item.FindControl("lblTotalOfFixedCapital");
                Label lblTotalOfWorkingCapital = (Label)item.FindControl("lblTotalOfWorkingCapital");
                Label lblTotalOfTurnover = (Label)item.FindControl("lblTotalOfTurnover");
                Label lblTotalOfTax = (Label)item.FindControl("lblTotalOfTax");
                Label lblTotalOfProfit = (Label)item.FindControl("lblTotalOfProfit");

                dv.RowFilter = groupField + "ID = " + hf.Value;
                dv.Sort = "CareerTitle ASC, Title ASC";
                gv.DataSource = dv;
                gv.DataBind();


                lblCountOfCooperative.Text = dv.Count.ToString("N0");

                object o = dv.ToTable().Compute("Sum (CooperatorQuantity)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblSumOfLabourQuantity.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số vốn điều lệ:
                o = dv.ToTable().Compute("Sum (CharterCapital)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfCharterCapital.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số vốn cố định:
                o = dv.ToTable().Compute("Sum (FixedCapital)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfFixedCapital.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số vốn kinh doanh:
                o = dv.ToTable().Compute("Sum (WorkingCapital)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfWorkingCapital.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số doanh thu:
                o = dv.ToTable().Compute("Sum (Turnover)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfTurnover.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số thuế:
                o = dv.ToTable().Compute("Sum (Tax)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfTax.Text = Convert.ToDecimal(o).ToString("N0");
                }

                // Tổng số lợi nhuận:
                o = dv.ToTable().Compute("Sum (Profit)", "Year = " + ddlHistoryYear.SelectedValue);
                if (o != DBNull.Value)
                {
                    lblTotalOfProfit.Text = Convert.ToDecimal(o).ToString("N0");
                }

                switch (groupField)
                {
                    case "AREA":
                        gv.Columns[4].Visible = false;
                        break;
                    case "CAREER":
                        gv.Columns[2].Visible = false;
                        break;
                    case "TYPE":
                        gv.Columns[3].Visible = false;
                        break;
                }
            }
        }

        protected void ddlGroupField_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Display(ddlGroupField.SelectedValue);
        }

        protected void ddlHistoryYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Display(ddlGroupField.SelectedValue);
        }
    }
}